package com.example.md;

import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;
import android.support.v4.app.NavUtils;
import com.remote_control_mobile.remote_control.R;

public class MouseActivity extends Activity {
	Button s_key;
	  int count=0;
	  int current_X=0;
	  int current_Y=0;
	  boolean ifTouched =false;

    @Override
  
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_next);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_next, menu);
        return true;
    }
  
    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
    	switch(event.getAction())
    	{
    		case MotionEvent.ACTION_DOWN:{
    			ifTouched = true;
    			current_X = (int)event.getX();
    			current_Y = (int)event.getY();
    		}
    		case MotionEvent.ACTION_UP:{
    			ifTouched = false;
    			//current_X=0;
    			//current_Y=0;
    		}
    		case MotionEvent.ACTION_MOVE:{
    		//	if(ifTouched)
    		//	{
    			int new_X = (int)event.getX();
    			int new_Y = (int)event.getY();
    			moveMouse( new_X - current_X , new_Y - current_Y);
    			current_X = new_X;
    			current_Y = new_Y;
    		//	}
    		}
    	}
    	return true;
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event)
    {
// do whatever you want to do here, then return true if you handled the key code
if (event.getAction() == KeyEvent.ACTION_DOWN) {
		
        switch (event.getKeyCode()) {
        	case KeyEvent.KEYCODE_0: sendKey( 48 ); break;
        	case KeyEvent.KEYCODE_1: sendKey( 49 ); break;
        	case KeyEvent.KEYCODE_2: sendKey( 50 ); break;
        	case KeyEvent.KEYCODE_3: sendKey( 51 ); break;
        	case KeyEvent.KEYCODE_4: sendKey( 52 ); break;
        	case KeyEvent.KEYCODE_5: sendKey( 53 ); break;
        	case KeyEvent.KEYCODE_6: sendKey( 54 ); break;
        	case KeyEvent.KEYCODE_7: sendKey( 55 ); break;
        	case KeyEvent.KEYCODE_8: sendKey( 56 ); break;
        	case KeyEvent.KEYCODE_9: sendKey( 57 ); break;
        	
        	case KeyEvent.KEYCODE_A: sendKey( 65 ); break;
        	case KeyEvent.KEYCODE_B: sendKey( 66 ); break;
        	case KeyEvent.KEYCODE_C: sendKey( 67 ); break;
        	case KeyEvent.KEYCODE_D: sendKey( 68 ); break;
        	case KeyEvent.KEYCODE_E: sendKey( 69 ); break;
        	case KeyEvent.KEYCODE_F: sendKey( 70 ); break;
        	case KeyEvent.KEYCODE_G: sendKey( 71 ); break;
        	case KeyEvent.KEYCODE_H: sendKey( 72 ); break;
        	case KeyEvent.KEYCODE_I: sendKey( 73 ); break;
        	case KeyEvent.KEYCODE_J: sendKey( 74 ); break;
        	case KeyEvent.KEYCODE_K: sendKey( 75 ); break;
        	case KeyEvent.KEYCODE_L: sendKey( 76 ); break;
        	case KeyEvent.KEYCODE_M: sendKey( 77 ); break;
        	case KeyEvent.KEYCODE_N: sendKey( 78 ); break;
        	case KeyEvent.KEYCODE_O: sendKey( 79 ); break;
        	case KeyEvent.KEYCODE_P: sendKey( 80 ); break;
        	case KeyEvent.KEYCODE_Q: sendKey( 81 ); break;
        	case KeyEvent.KEYCODE_R: sendKey( 82 ); break;
        	case KeyEvent.KEYCODE_S: sendKey( 83 ); break;
        	case KeyEvent.KEYCODE_T: sendKey( 84 ); break;
        	case KeyEvent.KEYCODE_U: sendKey( 85 ); break;
        	case KeyEvent.KEYCODE_V: sendKey( 86 ); break;
        	case KeyEvent.KEYCODE_W: sendKey( 87 ); break;
        	case KeyEvent.KEYCODE_X: sendKey( 88 ); break;
        	case KeyEvent.KEYCODE_Y: sendKey( 89 ); break;
        	case KeyEvent.KEYCODE_Z: sendKey( 90 ); break;
        	
        	// other characters
        	case KeyEvent.KEYCODE_DEL: sendKey( 8 ); break;			//backspace
        	case KeyEvent.KEYCODE_SPACE: sendKey( 32 ); break;			//" "
        	case KeyEvent.KEYCODE_APOSTROPHE: break; 	//"^"
        	case KeyEvent.KEYCODE_AT: sendKey( 512 ); break;				//"@"
        	case KeyEvent.KEYCODE_BACK: break;							//back
        	case KeyEvent.KEYCODE_BACKSLASH: sendKey( 92 ); break;		//"/"
        	case KeyEvent.KEYCODE_COMMA: sendKey( 44 ); break;			//","
        	case KeyEvent.KEYCODE_ENTER: sendKey( 10 ); break;							//enter
        	case KeyEvent.KEYCODE_EQUALS: sendKey( 61 ); break;			//"="
        	case KeyEvent.KEYCODE_GRAVE: sendKey( 128 ); break;			//"`"
        	case KeyEvent.KEYCODE_LEFT_BRACKET: sendKey( 91 ); break;	//"["
        	case KeyEvent.KEYCODE_MINUS: sendKey( 45 ); break;			//"-"
        	case KeyEvent.KEYCODE_PERIOD: sendKey( 46 ); break;			//"."
        	case KeyEvent.KEYCODE_PLUS: sendKey( 521 ); break;			//"+"
        	case KeyEvent.KEYCODE_POUND: break;			//"#"
        	case KeyEvent.KEYCODE_RIGHT_BRACKET: sendKey( 93 ); break;	//"]"
        	case KeyEvent.KEYCODE_SEMICOLON: sendKey( 59 ); break;		//";"
        	case KeyEvent.KEYCODE_SHIFT_LEFT: break;	//left
        	case KeyEvent.KEYCODE_SHIFT_RIGHT: break;	//right
        	case KeyEvent.KEYCODE_SLASH: sendKey( 47 ); break;			//"\"
        	case KeyEvent.KEYCODE_STAR: break;			//"*"
        	
        }
}
return super.dispatchKeyEvent(event);  // let the default handling take care of it
}

    private void sendKey( int key )
    {
    //	LoginActivity.myClient.sendMsg("keyboard press " + key +"" );
	//	LoginActivity.myClient.sendMsg("keyboard release " + key + "" );
    	LoginActivity.myClient.sendMsg("keyboard press " + key +"" );
		LoginActivity.myClient.sendMsg("keyboard release " + key + "" );
    }
    
    private void moveMouse( int x , int y ){
    	//LoginActivity.myClient.sendMsg("mouse move " + x + " " + y );
    	LoginActivity.myClient.sendMsg("mouse move " + x + " " + y );
    
    }
    
    private void pressMouse( int x ){
    	LoginActivity.myClient.sendMsg("mouse press " + x );
    }
    
    private void releaseMouse( int x ){
    	LoginActivity.myClient.sendMsg("mouse release " + x );
    }
    
	public void sendMessage(View view){
		//Toast.makeText(getApplicationContext(), "55555555", Toast.LENGTH_SHORT).show();
		
		Intent intent = new Intent(this,PPTActivity.class);

	
			
					/*hide the keyboard*/
				InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(view.getWindowToken(),0); 
			

	
		startActivity(intent);
	}
	
	public void leftClick(View view){
		pressMouse(0);
		releaseMouse(0);
	}
	
	public void rightClick(View view){
		pressMouse(1);
		releaseMouse(1);
	}
	
	public void showKeyboard(View view){
		Button s_key;//
		s_key=(Button) findViewById(R.id.button7);
		
		if(count%2==0){
		s_key.setText("Hide Keyboard");
		}
		else s_key.setText("Show Keyboard");
		count++;
		InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
		
	}

}
